import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class ListDistinct {
    public static void main(String[] args) {

        ListDistinct.listTest1();
    }

    /**
     * 可以通过hashset进行list去重但如果list存放的是自己声明的对象需要重写对象的hashcode和equals方法详见User
     * 如果不重写hashcode和equals的话就达不到去重的目的
     */
    public static void listTest() {
        User user1=new User(12,"111","111");
        User user2=new User(13,"111","111");
        User user3=new User(14,"111","111");
        User user4=new User(12,"111","111");
        ArrayList<User> list=new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        HashSet<User> hashSet=new HashSet<User>();
//        Set hashSet=new HashSet<>(list);
//        List list1=new ArrayList<>();
        for (int i = 0; i <list.size() ; i++) {
            hashSet.add(list.get(i));
        }
        for (User u:hashSet) {
            System.err.println(u.toString());
        }
        System.err.println(hashSet.toString());



        Set<User> set = new HashSet<User>();

        set.add(new User(1,"你","1"));
        set.add(new User(2,"你","1"));
        set.add(new User(3,"你","1"));


        // 重复数据添加失败

        set.add(new User(1,"你","1"));



        for(User s: set){

            System.out.println(s.getAge());

        }
    }
    public static void listTest1() {
        User user1=new User(12,"111","111");
        User user2=new User(13,"111","111");
        User user3=new User(14,"111","111");
        User user4=new User(12,"111","111");
        Set<User> set = new HashSet<>();
        set.add(user1);
        set.add(user2);
        set.add(user3);
        set.add(user4);
        for (User user: set
             ) {
            System.out.println(user.getAge());
        }
    }

    /**
     * 利用contains判断进行去重(有序)
     */
    @Test
    public void listquchong(){
        ArrayList<Integer> list =new ArrayList<Integer>();
            list.add(1);
            list.add(2);
            list.add(3);
            list.add(4);
            list.add(2);
            list.add(1);
            list.add(6);;
        System.err.println("原集合"+list);
        ArrayList newList=new ArrayList<Integer>();
        for (Integer li :list) {
            if (!newList.contains(li)){
                newList.add(li);
            }
        }
        System.err.println("new"+newList);
    }

    /**
     * 利用HashSet进行去重(无序) hashset自动排序打乱了原有的排序
     */
    @Test
    public void listByHashSet(){
        ArrayList<Integer> list =new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(2);
        list.add(1);
        list.add(6);;
        System.err.println("原集合"+list);
        HashSet<Integer> hashSet=new HashSet<>(list);
        System.err.println("new"+hashSet);
    }

    /**
     * 利用LinkedHashSet进行去重 有序 推荐使用该方法进行去重
     */
    @Test
    public void listByLinkedHashSet(){
        ArrayList<Integer> list =new ArrayList<Integer>();
        list.add(1);
        list.add(3);
        list.add(2);
        list.add(4);
        list.add(2);
        list.add(1);
        list.add(6);;
        System.err.println("原集合"+list);
        LinkedHashSet<Integer> hashSet=new LinkedHashSet<>(list);
        System.err.println("new"+hashSet);
    }

    /**
     * 利用jdk1.8的新特性Stream进行去重 有序 推荐使用
     */
    @Test
    public void listByStream(){
        List<Integer> list =new ArrayList<Integer>();
        list.add(1);
        list.add(3);
        list.add(2);
        list.add(4);
        list.add(2);
        list.add(1);
        list.add(6);;
        System.err.println("原集合"+list);
        list=list.stream().distinct().collect(Collectors.toList());
        System.err.println("new"+list);
    }
}
